function [datatable header] = TfcToDatatable(tfc, subjectinfo)
header = {'Subject', 'Condition', 'Channel', 'Region', 'Bin', 'FrequencyMin', 'FrequencyMax', 'Location', 'Latency (ms)', 'Type', 'Value', 'P', 'F30p Location', 'F30p Latency', 'F30p Skipped'};
datatable = {};
currentRow.Subject = tfc.subject;
currentRow.Condition = tfc.epochs;
channels = fieldnames(tfc.data);
for i=1:length(channels)
    channel = cell2mat(channels(i));
    currentRow.Channel = channel;
    currentRow.Region = channel; % TODO: This needs to be changed to region, as soon as info on the electrodes is available somewhere
    for j=1:length(tfc.data.(channel));
        d = tfc.data.(channel)(j);
        % Find the bin description
        currentRow.Bin = tfc.freqbindescriptions{tfc.freqbins(:,1)==d.frequencyrange(1) & tfc.freqbins(:,2)==d.frequencyrange(2)};
        currentRow.BinFrequencyMin = d.frequencyrange(1);
        currentRow.BinFrequencyMax = d.frequencyrange(2);
        for p = 1:length(d.peaklocs)
            currentRow.Location = d.peaklocs(p);
            currentRow.Latency = tfc.times(currentRow.Location);
            currentRow.Type = d.peaktype(p);
            currentRow.Value = d.mean(currentRow.Location);
            currentRow.P = d.pvalue(p);
            firstEffect = d.firstEffect(p);
            currentRow.F30p_Location = firstEffect.percent30_location;
            currentRow.F30p_Latency = firstEffect.percent30_latency;
            currentRow.F30p_Skipped = firstEffect.percent30_peaksSkipped;
            
            % Write the data to the table..
            datatable(end+1,:) = { ...
                currentRow.Subject, ...
                currentRow.Condition, ...
                currentRow.Channel, ...
                currentRow.Region, ...
                currentRow.Bin, ...
                currentRow.BinFrequencyMin, ...
                currentRow.BinFrequencyMax, ...
                currentRow.Location, ...
                currentRow.Latency, ...
                currentRow.Type, ...
                currentRow.Value,...
                currentRow.P,...
                currentRow.F30p_Location,...
                currentRow.F30p_Latency,...
                currentRow.F30p_Skipped};
       end
    end
end
end